package com.fitso.model.dao.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;

/**
 * Profiles Dao aspects. This can be used to monitor the execution time of Dao
 * processes.
 * 
 * @author timothystorm
 */
@Aspect
public class DaoProfileAspect {
	private static final Logger logger = Logger.getLogger(DaoProfileAspect.class);

	@Around("execution(* com.fitso.model.dao.*.*(..))")
	public Object profile(ProceedingJoinPoint pjp) throws Throwable {
		StopWatch sw = new StopWatch();
		String name = pjp.getSignature().getName();

		sw.start(name);
		Object returnValue = pjp.proceed();
		sw.stop();

		logger.info(String.format("execution of %s took: %dms", name, sw.getTotalTimeMillis()));
		return returnValue;
	}
}
